﻿// -----------------------------------------------------------------------
//  <copyright file="ProductController.cs" company="柳柳软件">
//      Copyright (c) 2014-2015 66SOFT. All rights reserved.
//  </copyright>
//  <site>http://www.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2015-12-08 0:56</last-date>
// -----------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web.Mvc;

using OSharp.Core.Data.Extensions;
using OSharp.Utility;
using OSharp.Utility.Data;
using OSharp.Utility.Filter;
using OSharp.Web.Mvc;
using OSharp.Web.Mvc.Extensions;
using OSharp.Web.Mvc.UI;

using ZH.JY.Store;
using ZH.JY.Store.Dtos;
using ZH.JY.Store.Models;


namespace ZH.JY.Web.Areas.Admin.Controllers
{
    [Description("管理-商品信息")]
    public class ProductsController : BaseController
    {
        public IStoreContract StoreContract { get; set; }

        [Description("管理-商品信息-读取")]
        public ActionResult Read()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<Product, bool>> predicate = FilterHelper.GetExpression<Product>(request.FilterGroup);
            var page = StoreContract.Products.ToPage(predicate,
                request.PageCondition,
                m => new
                {
                    m.Id,
                    m.Name,
                    m.ProductType,
                    m.Milage,
                    m.Count,
                    m.CreatedTime
                });
            return Json(page.ToGridData());
        }

        [Description("管理-产品信息-节点读取")]
        public ActionResult NodeRead(ProductType? type)
        {
            var nodes = (type.HasValue ? StoreContract.Products.Where(m => m.ProductType == type.Value) : StoreContract.Products)
                .OrderBy(m => m.Name).Select(m => new
                {
                    ProductId = m.Id,
                    ProductName = m.Name
                }).ToList();
            return Json(nodes, JsonRequestBehavior.AllowGet);
        }

        [Description("管理-商品信息-新增")]
        [HttpPost]
        public async Task<ActionResult> Create(ProductInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = await StoreContract.CreateProducts(dtos);
            return Json(result.ToAjaxResult());
        }

        [Description("管理-商品信息-更新")]
        [HttpPost]
        public async Task<ActionResult> Update(ProductInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = await StoreContract.UpdateProducts(dtos);
            return Json(result.ToAjaxResult());
        }

        [Description("管理-商品信息-删除")]
        [HttpPost]
        public async Task<ActionResult> Delete(int[] ids)
        {
            ids.CheckNotNull("ids");
            OperationResult result = await StoreContract.DeleteProducts(ids);
            return Json(result.ToAjaxResult());
        }
    }
}